<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/16
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Owner;
use think\Db;
use think\Config;

/**
 * 城市快运🚐
 */
class Order2 extends Owner
{
//获取表_订单类型:1=普通发货,2=城市快运,3=拉货搬家,4=专线
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $db = 'order2';


    // /**
    //  * 搜索
    //  *
    //  * @param string $str 搜索内容
    //  * @return void
    //  */
    // public function test($str = 'xiao')
    // {
    //     $where['username|nickname'] = ['LIKE','%'.$str.'%'];
    //     $list = Db::name('user')
    //         ->where($where)
    //         ->select();
    //     $this->success('ok',$list);
    // }


     /**
     * 发货人下单📦
     *
     * @param int $s_addr_id 装货地址
     * @param int $e_addr_id 卸货地址
     * @param int $s_load_time 装货开始时间        
     * @param string $type_status  运输类型:1=运货,2=搬家
     * @param string $s_ps 发货人备注信息            
     * @param int $car_id 车id
     * @param string $goods_name 货物名称
     * @param string $follow 跟车人数
     * @param string $tag2_ids 标签2ids
     * @param decimal $km 预计公里数
     * @param decimal $money 估计价格
     * @param string $attach 附加服务(需要搬运/返程)
     * 
     * @return void
     */
    public function add_order(  $s_addr_id = null,
    
            $s2_addr_id = null,
            $e_addr_id = null,
            $e2_addr_id = null,
            $s_load_time = null,
            $e_load_time = null,
            $type_status = null,
            $s_ps = null,
            $attach = null,
            $goods_name = null,
            $long = null,
            $wide = null,
            $high = null,$tag2_ids=null,$km=null,$money=null,$follow=null,$car_id=null){
 
        // $params = $this->request->param();
        //     $fields = [   ];

        //    * @param string $load_status 装车类型:1=整车,2=零担
        //     * @param decimal $weight 吨(零担必传)
        //     * @param decimal $wide 宽
    //  * @param decimal $high 高
    // * @param int $e_load_time 装货结束时间()    
    // * @param int $e2_addr_id 卸货地址2  
    // * @param int $s2_addr_id 装货地址2

        $data['s_addr_id'] = $s_addr_id;
        // $data['s2_addr_id'] = $s2_addr_id;
        $data['e_addr_id'] = $e_addr_id;
        // $data['e2_addr_id'] = $e2_addr_id;
        $data['s_load_time'] = strtotime($s_load_time);
        // $data['e_load_time'] = strtotime($e_load_time);
        $data['type_status'] = $type_status;
        $data['s_ps'] = $s_ps;
        // $data['load_status'] = $load_status;
        $data['goods_name'] = $goods_name;
        // $data['long'] = $long;
        // $data['wide'] = $wide;
        // $data['high'] = $high;
        // $data['weight'] = $weight;
        $data['tag2_ids'] = $tag2_ids;
        $data['car_id'] = $car_id;

        $data['money'] = $money;
        $data['follow'] = $follow;
        $data['km'] = $km;

        $data['s_money'] = Config::get('site.s_money2');
        $data['j_money'] = Config::get('site.j_money2');

        //     $data = $this->get_field_arr($params,$fields,true);
            $data['s_uid'] = $this->auth->id;
            
            $data['status'] = '1';//'状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            // $data['lay_status'] = '1';//装卸类型:1=一装一卸,2=一装两卸,3=两装一卸,4=两装两卸',

            
            $this->AddAddrUseNum($data['s_addr_id']);
            $this->AddAddrUseNum($data['e_addr_id']);
            // if(!empty($data['s2_addr_id'])){
            //     $this->AddAddrUseNum($data['s2_addr_id']);
            //     if(!empty($data['e2_addr_id'])){
            //         $data['lay_status'] = '4';
            //         $this->AddAddrUseNum($data['e2_addr_id']);
            //     }else{
            //         $data['lay_status'] = '3';
            //     }
            // }elseif(!empty($data['e2_addr_id'])){
            //     $data['lay_status'] = '2';
            //     $this->AddAddrUseNum($data['e2_addr_id']);
            // }
            $data['order'] = $this->rand_order('Order2',$this->auth->id);

            // switch ($data['money_status']) {//费用方案:1=平台估价,2=货主出价,3=电话议价
            //     case '1':
                  
            //         //装车类型:1=整车,2=零担
            //         switch ($data['load_status']) {
            //             case '1':
            //                 // $this->Long_Wide_High_GetBulkloadPrice($order,$long ,$wide ,$high ,$km  ,$t )
            //                 break;
            //             case '2':
            //                 // $this->Long_Wide_High_GetBulkloadPrice($order,$long ,$wide ,$high ,$km  ,$t )
            //                 break;
                        
            //             default:
            //                 # code...
            //                 break;
            //         }
            //         break;
                
            //     default:
            //         # code...
            //         break;
            // }
            // $data['size'] = $long * $wide * $weight;

            $data['time'] = time();
            $data['id'] = Db::name($this->db)->insertGetId($data);

            $this->success('请选择支付方式',$data);

            //支付订单
            $pay_data = $this->PayOrder($data['id'],2,$this->auth->id,'1');
            $this->success('pay',$pay_data);

    }


    /**
     * 发货人支付未支付的订单💰
     *
     * @param string $order 订单号
     * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他默认3
     * @return void
     */
    public function s_pay_order($order,$pay_type='3')
    {
        $data = Db::name($this->db)->where('order',$order)->find();
        if(!$data){
            $this->error('没有此订单');
        }elseif($data['spay_status']!='2'){//发货人支付押金:1=已支付,2=未支付,3=已违约
            $this->error('已支付');
        }
        $res = $this->PayOrder($data['id'],2,$this->auth->id,'1',$pay_type);
        $this->success('pay',$res);
    }






    /**
     * 获取发货列表
     *
     * @param integer $page 1
     * @param integer $limit 5
     * @param integer $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function get_order_list($page=1,$limit=5,$status=null)
    {
        $where['s_uid'] = ['=',$this->auth->id];
        if($status){
            $where['status'] = ['=',$status];
        }
        $list = Db::name($this->db)->where($where)->order('time desc')->page($page,$limit)->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder2FieldData($val);           
                if($val['j_uid']){
                    $val['user'] = $this->GetJuserData($val['j_uid']);
                }      
            }
        }
        $this->success('ok',$list);
    }


    /**
     * 获取待完成订单列表
     *
     * @param integer $page 1
     * @param integer $limit 5
     * @return void
     */
    public function get_todo_order_list($page=1,$limit=5)
    {
        $where['s_uid'] = ['=',$this->auth->id];
    
        $where['status'] = ['<',6];//$status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
        $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
        $list = Db::name($this->db)->where($where)->order('time desc')->page($page,$limit)->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder2FieldData($val);   
                if($val['j_uid']){
                    $val['user'] = $this->GetJuserData($val['j_uid']);
                }                
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取城市快运订单信息
     * 
     * @param int $id 订单id
     * @return void
     */ 
    public function get_order2_data($id)
    {
        $val = $this->GetOrder2Data($id);
        if($val['j_uid']){
            $val['user'] = $this->GetJuserData($val['j_uid']);
        }  
        $this->success('ok',$val);
    }


    /**
     * 用户更新订单2状态
     *
     * @param int $id 订单号id
     * @param int $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function update_order2_status($id,$status)
    {
        $where['s_uid'] = $this->auth->id;
        $where['id'] = $id;
        $res = Db::name($this->db)
                ->where($where)
                ->find();
        if(!$res){
            $this->error('没有该订单');
        }
        switch ($status) {
            case '1':
                // if($res['status']!='9')
                // $this->error('当前订单待接单');
                break;

            case '2':
                // if($res['status']!='9')
                $this->error('当前已接单,不能修改装');
                break;

            case '3':
                if($res['status']!='2')
                $this->error('未接单');
                
                break;

            case '4':         
                if($res['status']!='3')      
                $this->error('司机未到达目的地');
                
                break;

            case '5':
                if($res['status']!='4')
                $this->error('货主未确认已装车');
                
                break;

            case '6':     
                if($res['status']!='5')
                $this->error('司机未确认送达');
                break;
        //1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            default:
                $this->error('暂未加入此状态');
                break;
        }
        $this->UpdateOrder2Status($id,$status);

        $this->success('修改成功');
    }

    /**
     * 修改订单状态
     *
     * @param int $id 订单id
     * @param int $status 状态
     * @return void
     */
    protected function UpdateOrder2Status($id,$status){

        //后期加入ws推送给司机
        Db::name($this->db)->where('id',$id)->update(['status'=>$status]);
    }



}
